package com.joint.cloud.auth.config.service;

import com.joint.cloud.auth.config.custom.CustomUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.security.oauth2.common.exceptions.InvalidClientException;
import org.springframework.security.oauth2.provider.NoSuchClientException;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.List;


/**
 * @description:
 * @author: super.wu
 * @date: Created in 2018/5/25 0025
 * @modified By:
 * @version: 1.0
 **/
@Repository
public class JdbcUserDetailsService {

    private String selectUserDetailsSql;

    private String selectUserDetailsRoleSql;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public JdbcUserDetailsService() {
        this.selectUserDetailsSql = "select u.user_id as userId, u.login_name as username, u.show_name as name,u.password,u.user_type,u.head_image_url,u.dept_id ,u.user_status  from zz_sys_user AS u where login_name = ? and  deleted_flag = 1";
        this.selectUserDetailsRoleSql = "select r.role_id FROM zz_sys_user u,zz_sys_user_role ur, zz_sys_role r WHERE u.user_id=ur.user_id AND r.role_id=ur.role_id AND u.login_name = ?";
    }

    public CustomUserDetails loadUserDetailsByUserName(String username) throws InvalidClientException {
        try {
            CustomUserDetails details = this.jdbcTemplate.queryForObject(this.selectUserDetailsSql, new UserDetailsRowMapper(), new Object[]{username});
            return details;
        } catch (EmptyResultDataAccessException var4) {
            throw new NoSuchClientException("No client with requested username: " + username);
        }
    }

    public List<String> loadUserRolesByUserId(Long userId) {
        List<String> roles = this.jdbcTemplate.query(this.selectUserDetailsRoleSql, new RoleRowMapper(), new Object[]{userId, userId});
        return roles;
    }

    public List<String> loadUserRolesByUsername(String username) {
        List<String> roles = this.jdbcTemplate.query(this.selectUserDetailsRoleSql, new RoleRowMapper(), new Object[]{username});
        return roles;
    }

    private static class UserDetailsRowMapper implements RowMapper<CustomUserDetails> {

        @Override
        public CustomUserDetails mapRow(ResultSet resultSet, int i) throws SQLException {
            CustomUserDetails customUserDetails = new CustomUserDetails();
            customUserDetails.setUserId(resultSet.getString(1));
            customUserDetails.setUsername(resultSet.getString(2));
            customUserDetails.setPassword(resultSet.getString(4));
            customUserDetails.setEnabled(true);
            customUserDetails.setAuthorities(new HashSet<>());
            customUserDetails.setName(resultSet.getString(3));
            return customUserDetails;
        }

    }

    private static class RoleRowMapper implements RowMapper<String> {
        @Override
        public String mapRow(ResultSet resultSet, int i) throws SQLException {
            return resultSet.getString(1);
        }
    }

}
